package com.example.fairplay.newfpregistration.ui;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.fairplay.newfpregistration.R;
import com.example.mylibrary.MyApp;
import com.example.mylibrary.config.Config;
import com.example.mylibrary.data.DataDao;
import com.example.mylibrary.data.StudentInfo;
import com.example.mylibrary.tcp.PackageHeadInfo;
import com.example.mylibrary.tcp.ResultPackage;
import com.example.mylibrary.tcp.SocketClient;
import com.example.mylibrary.tcp.TCPConst;
import com.example.mylibrary.util.ActivityCollector;
import com.example.mylibrary.util.ImageUtil;
import com.example.mylibrary.util.Logger;
import com.example.mylibrary.util.ResultCode;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.com.aratek.facelib.AraFaceAuthLib;
import cn.com.aratek.fp.Bione;
import cn.com.aratek.fp.FingerprintImage;
import cn.com.aratek.fp.FingerprintScanner;
import cn.com.aratek.idcard.IDCard;
import cn.com.aratek.idcard.IDCardReader;
import cn.com.aratek.util.Result;

import static com.example.mylibrary.tcp.PackageHeadInfo.g_SepFlag1;
import static com.example.mylibrary.tcp.TCPConst.MAX_RECORD_COUNT;


public class MainCheckActivity extends AppCompatActivity {
    private static final String FP_DB_PATH = "/sdcard/FP-KTA2203/fp.db";
    private static final String PATH = Environment
            .getExternalStorageDirectory() + "/FP-KTA2203/Photo/";
    public static final String FILE_IMG ="/sdcard/FP-KTA2203/img/";

    private static final int MSG_FP_RESULT = 0;
    private static final int MSG_SHOW_INFO = 1;
    private static final int MSG_CARD_INFO = 2;
    private static final int MSG_SHOW_PROGRESS_DIALOG = 3;
    private static final int MSG_DISMISS_PROGRESS_DIALOG = 4;
    private static final int MSG_RESULT_INFO = 7;
    private static final int MSG_UPDATE_IMAGE = 5;
    private static final int MSG_FACE_RESULT = 8;
    private static final int MSG_VERIFY_INFO = 9;
    private static final int MSG_FP_INSERT_DATA = 10;
    private static final String TAG = "MainCheckActivity";
    private boolean isReading = true;
    private IDCardReader mReader;
    private IDCard idCard;
    private Bitmap Sbitmap = null;
    private byte[] cardData = null;
    private AraFaceAuthLib araFaceAuthLib;
    private ProgressDialog mProgressDialog;
    private FingerprintScanner mScanner;
    private StudentInfo studentInfo;
    private MyApp app;
    private boolean isOpenFinger = true;
    private boolean isOpenFail= false;
    private Logger logger;

    //绑定控件
    @Bind(R.id.ed_name)
    EditText editText_name;
    @Bind(R.id.ed_sex)
    EditText editText_sex;
    @Bind(R.id.ed_nation)
    EditText editText_nation;
    @Bind(R.id.ed_idcard)
    EditText editText_idcard;
    @Bind(R.id.ed_birthday)
    EditText editText_birthday;
    @Bind(R.id.ed_address)
    EditText editText_address;
    @Bind(R.id.ed_school)
    EditText editText_school;
    @Bind(R.id.ed_stuID)
    EditText editText_stuId;
    @Bind(R.id.img_Scene_photo)
    ImageView img_Scenephoto;
    @Bind(R.id.img_idcard_photo)
    ImageView img_idfacephoto;
    @Bind(R.id.rl_FP)
    RelativeLayout fprelativeLayout;
    @Bind(R.id.rl_faca)
    RelativeLayout facerelativeLayout;
    @Bind(R.id.tv_comparison1)
    TextView comparisontext;
    @Bind(R.id.tv_Fp_results1)
    TextView fp_results1;
    //@Bind(R.id.img_fp)
   // ImageView fpICO;
    @Bind(R.id.tv_ip)
    TextView ipText;
    @Bind(R.id.tv_ip_text)
    TextView ipTextData;
    @Bind(R.id.tv_local)
    TextView localText;
    @Bind(R.id.tv_local_text)
    TextView localTextData;
    @Bind(R.id.tv_state)
    TextView stateText;
    @Bind(R.id.tv_state_text)
    TextView stateTextData;


    private List<StudentInfo> studentInfos;
    private int FPResult = -99 ;
    private byte[] fpCardData = null;
    private byte[] fpEnorllData = null;
    private File fic;
    private File fss;
    private ResultPackage rcPackage;
    private SharedPreferences sharedPreferences;
    private String ip;
    private String local;
    private String idinfo;
    private String idinfos;
    private String stuCode;
    private String stuCode1;
    private String show_id;
    private SimpleDateFormat formatter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        SharedPreferences sp=getSharedPreferences("data",0);
        ip = sp.getString("Ip", "");
        local = sp.getString("local", "");

        formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        initDatas();
        initViews();
        Log.e(TAG, "idcardNumH:-----------> "+ stuCode1);


    }

    private void initViews() {
        ipText.bringToFront();
        ipTextData.bringToFront();
       localText.bringToFront();
       localTextData.bringToFront();
       stateText.bringToFront();
       stateTextData.bringToFront();
    //    Intent faceintent=getIntent();
//        if(faceintent!=null)
//        {
//            Bitmap  bitmap=faceintent.getParcelableExtra("sceneface");
//            img_idfacephoto.setImageBitmap(bitmap);
//        }



    }
    @OnClick({R.id.btn_pass,R.id.btn_not_pass})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.btn_pass:
                Toast.makeText(this, "通过", Toast.LENGTH_SHORT).show();
                //  Date curDate    =   new    Date(System.currentTimeMillis());
                rcPackage = new ResultPackage();
                FPResult = ResultCode.VERIFY_PASS;
                studentInfo.setRollCallState(FPResult);
                studentInfo.setFinalRollCalltime(formatter.format(new Date()));
                //传值
               giveValue();
              //  studentInfo.setUploadCode(FPResult);
                DataDao.updateInfo(studentInfo);

                if (MyApp.mAcache2.getAsString("是否开启分组") != null) {
                    //开启
                    if (Boolean.valueOf(MyApp.mAcache2.getAsString("是否开启分组"))) {
                        //开启状态
                        Intent intent = new Intent(MainCheckActivity.this,ItemGropActivity.class);
                        startActivity(intent);

                    } else {
                        Intent intent = new Intent(MainCheckActivity.this,TransitionActivity.class);
                        startActivity(intent);
                    }
                }


               this.finish();
                break;
            case R.id.btn_not_pass:
                Toast.makeText(this, "未通过", Toast.LENGTH_SHORT).show();
                Intent intent1 = new Intent(MainCheckActivity.this,TransitionActivity.class);
                startActivity(intent1);
                this.finish();
                break;

        }

    }



    //获取数据及绑定
    private void initView() {

        //获取输入的身份证号码
        Intent intent = getIntent();
            Bundle bundle = intent.getExtras();
            idinfo = bundle.getString("Idcard");
            idinfos = bundle.getString("idcardinfo");
             Log.i(TAG, "idinfos: "+ idinfos);
            stuCode = bundle.getString("scanner");
            stuCode1 = getIntent().getStringExtra("stuCode");
        Log.e(TAG, "idcardNum1:-----------> "+ stuCode1);

        if(idinfo !=null){
            studentInfos =DataDao.queryInfo(idinfo);
            if (studentInfos == null || studentInfos.size() < 1) {
                studentInfos =DataDao.queryStuCode(idinfo);
            }

        }else if(idinfos != null){
            openDevice();
            studentInfos =  DataDao.queryInfo(idinfos);

        }else if(stuCode !=null) {
            studentInfos =  DataDao.queryStuCode(stuCode);
        }else if(stuCode1 != null){

            studentInfos =DataDao.queryStuCode(stuCode1);

        }else {
            Intent intent2 = new Intent(MainCheckActivity.this,TransitionActivity.class);
            startActivity(intent2);
           this.finish();
        }

        //根据身份证查找出信息
        for (int i = 0;i<studentInfos.size();i++) {
            studentInfo = studentInfos.get(i);
        }
        Log.e(TAG, "initView: "+studentInfo.getStudentName());
        //绑定控件
        editText_name.setText(studentInfo.getStudentName());
        editText_sex.setText(studentInfo.getSex()== 0?"男":"女");
        editText_birthday.setText(studentInfo.getBirthday());
        editText_nation.setText(studentInfo.getNation());
        editText_address.setText(studentInfo.getAddress());
        if(studentInfo.getIdCard() != null && studentInfo.getIdCard().length() == 18){
            show_id = studentInfo.getIdCard().substring(0, 3) + "********" + studentInfo.getIdCard().substring(11);
        }
        editText_idcard.setText(show_id);

        editText_school.setText(studentInfo.getSchool());
        editText_stuId.setText(studentInfo.getStudenCode());
        //分组比赛开始时间唯一标识，用于回包删除已发送包

        //获取本地学生照片

        File file = new File(FILE_IMG+studentInfo.getStudenCode()+".jpg");
        Log.e(TAG, "img-->: "+file );

        if (file.exists()) {
            Bitmap bm = BitmapFactory.decodeFile(file.getAbsolutePath());
            //将图片显示到ImageView中
           img_idfacephoto.setImageBitmap(bm);

        }


        byte[] snapshotFeature = getIntent().getByteArrayExtra("snapshotFeature");
        if (snapshotFeature != null) {
            final Bitmap b = ImageUtil.getRotateBitmap(BitmapFactory
                            .decodeByteArray(snapshotFeature, 0, snapshotFeature.length),
                    Config.displayOrientation);
            img_Scenephoto.setImageBitmap(b);
            fp_results1.setVisibility(View.VISIBLE);

            comparisontext.setVisibility(View.VISIBLE);
            comparisontext.setText(R.string.fp_comparison_pass);
            comparisontext.setTextColor(this.getResources().getColor(R.color.green));
        }





    }

    //初始化数据
    private void initDatas() {
        ipTextData.setText(ip);
        localTextData.setText(local);
     //   stateTextData.setText(R.string.tcp_Connectionless);

        mProgressDialog = new ProgressDialog(this);
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        mProgressDialog.setIcon(android.R.drawable.ic_dialog_info);
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.setCancelable(false);
        mReader = IDCardReader.getInstance(this);
        mScanner = FingerprintScanner.getInstance(this);
        studentInfo = new StudentInfo();
        app = MyApp.getInstance();
        creatFile();

        mHandler.sendMessage(mHandler.obtainMessage(MSG_CARD_INFO));
        //打开设备


    }
    private void creatFile() {
        fic = new File(PATH + "IDCard");
        if (!fic.exists()) {
            try {
                fic.mkdirs();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        fss = new File(PATH + "Snapshot");
        if (!fss.exists()) {
            try {
                fss.mkdirs();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    //打开身份证设备
    private void openDevice() {
        new Thread() {
            @Override
            public void run() {
                int error;
                showProgressDialog(getString(R.string.loading), getString(R.string.preparing_device));
                if (isOpenFinger) {
                    if ((error = mScanner.powerOn()) != FingerprintScanner.RESULT_OK) {
                        dismissProgressDialog();
                        isOpenFail = true;
                        showInfoToast(getString(R.string.fp_power_on_failed));
                        return;
                    }
                    if ((error = mScanner.open()) != FingerprintScanner.RESULT_OK) {
                        dismissProgressDialog();
                        isOpenFail = true;
                        showInfoToast(getString(R.string.fp_open_failed));
                        return;
                    }
                    if ((error = Bione.initialize(MainCheckActivity.this, FP_DB_PATH)) != Bione.RESULT_OK) {
                        dismissProgressDialog();
                        showInfoToast(getString(R.string.error_initialization_failed));
                        return;
                    }
                }
                dismissProgressDialog();
                       showInfoToast(getString(R.string.device_open_success));

            }
        }.start();
    }
    //显示对话框
    private void showProgressDialog(String title, String message) {
        mHandler.sendMessage(mHandler.obtainMessage(MSG_SHOW_PROGRESS_DIALOG,
                new String[] { title, message }));
    }
    private void dismissProgressDialog() {
        mHandler.sendMessage(mHandler
                .obtainMessage(MSG_DISMISS_PROGRESS_DIALOG));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        closeDevice();
        mHandler.removeCallbacks(mRunnable);

    }
    //关闭设备
    private void closeDevice() {
        new Thread() {
            @Override
            public void run() {
                int error;
                isReading = false;
                if (isOpenFinger) {
                    if ((error = Bione.exit()) != Bione.RESULT_OK) {
                        showInfoToast(getString(R.string.error_algorithm_cleanup_failed));
                        dismissProgressDialog();
                        return;
                    }
                    if ((error = mScanner.close()) != FingerprintScanner.RESULT_OK) {
                     //   showInfoToast(getString(R.string.fp_close_failed));
                        dismissProgressDialog();
                        return;
                    }
                    if ((error = mScanner.powerOff()) != FingerprintScanner.RESULT_OK) {
                      //  showInfoToast(getString(R.string.fp_power_off_failed));
                        dismissProgressDialog();
                        return;
                    }
                }

                showInfoToast(getString(R.string.device_close_success));
            }
        }.start();
    }
    private void clearFeature(){
        cardData = null;
        Sbitmap = null;

    }
    private void showInfoToast(String info) {
        mHandler.sendMessage(mHandler.obtainMessage(MSG_SHOW_INFO, info));
    }
    //比对指纹特征
    private void Distinguish() {

        new Thread(){

            public void run() {
                showProgressDialog(getString(R.string.loading),
                        getString(R.string.please_press_finger));
                Result res;

                mScanner.prepare();
                do {
                    res = mScanner.capture();
                } while (res.error != FingerprintScanner.RESULT_OK);
                mScanner.finish();
                if (res.error == FingerprintScanner.RESULT_OK) {
                    FingerprintImage fi = (FingerprintImage) res.data;
                    res = Bione.extractFeature(fi);
                    updateFingerprintImage(fi);
                    if (res.error != Bione.RESULT_OK) {
                        showInfoToast(getString(R.string.error_invalid_feature_data));
                        dismissProgressDialog();
                        return;
                    }
                    fpEnorllData = (byte[]) res.data;

                    Log.e("fpCardData ", Arrays.toString(fpCardData));
                    Log.e("fpEnorllData ", Arrays.toString(fpEnorllData));
                    Result vRes = Bione.idcardVerify(fpCardData, fpEnorllData);
                    Log.e("error", String.valueOf(vRes.error));
                    Log.e("data", String.valueOf(vRes.data));
                    mHandler.sendMessage(mHandler.obtainMessage(MSG_FP_RESULT,
                            vRes));
                } else {
                    showInfoToast(getString(R.string.error_operation_failed));
                }
            };
        }.start();

    }
    //显示指纹图像
    private void updateFingerprintImage(FingerprintImage fi) {
        byte[] fpBmp = null;
        Bitmap bitmap;
        if (fi == null || (fpBmp = fi.convert2Bmp()) == null || (bitmap = BitmapFactory.decodeByteArray(fpBmp, 0, fpBmp.length)) == null) {
            bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.nofinger);
        }
        mHandler.sendMessage(mHandler.obtainMessage(MSG_UPDATE_IMAGE, bitmap));
    }
    //加载menu资源
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_actionbar, menu);
        return super.onCreateOptionsMenu(menu);

    }
    //actionbar点击事件
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.btn_pass:
                Toast.makeText(this, "通过", Toast.LENGTH_SHORT).show();
                //  Date curDate    =   new    Date(System.currentTimeMillis());
                rcPackage = new ResultPackage();
                FPResult = ResultCode.VERIFY_PASS_NO_FP;
                studentInfo.setRollCallState(FPResult);
                studentInfo.setFinalRollCalltime(formatter.format(new Date()));
                DataDao.updateInfo(studentInfo);
                //传值
                giveValue();
                Intent intent = new Intent(MainCheckActivity.this,TransitionActivity.class);
                startActivity(intent);
                this.finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        startActivity(new Intent(this,MainMenu.class));
        ActivityCollector.removeActivity(this);
        finish();
    }

    private void clearData() {
        FPResult = -99;
    }

    private Handler mHandler =new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case MSG_SHOW_INFO:
                    Toast.makeText(MainCheckActivity.this, (String) msg.obj,
                            Toast.LENGTH_SHORT).show();
                    break;
                case MSG_CARD_INFO:
                    Log.e(TAG, "idcardNum111:-----------> "+ stuCode1);

                    initView();
                    Intent intents = getIntent();
                    Bundle bundle = intents.getExtras();
                    byte[] fpinfo =  bundle.getByteArray("fpinfo");
                    //      img_idcardface.setImageBitmap(idCard.getPhoto());
                    //      img_idfacephoto.setImageBitmap(idCard.getPhoto());
                    //身份证是否有指纹 没有指纹则关闭设备
                    if(fpinfo !=null){
                        fpCardData = fpinfo;
                        Distinguish();
                    }else {
                        //关闭设备
                    //    comparisontext.setText(R.string.fp_comparison_no_fp);

                        //  closeDevice();
                    }

                    //  mHandler.sendMessage(mHandler.obtainMessage(MSG_INSERT_DATA,idCard));
                    break;
                case MSG_SHOW_PROGRESS_DIALOG: {
                    String[] info = (String[]) msg.obj;
                    mProgressDialog.setTitle(info[0]);
                    mProgressDialog.setMessage(info[1]);
                    mProgressDialog.show();
                    break;
                }
                case MSG_DISMISS_PROGRESS_DIALOG: {
                    mProgressDialog.dismiss();
                    break;
                }
                case MSG_UPDATE_IMAGE:
                    //指纹图像
                    img_Scenephoto.setImageBitmap((Bitmap) msg.obj);
                    break;
                case MSG_FP_RESULT:
                    //指纹
                    Result res = (Result) msg.obj;
                    //比对成功
                    if (res.error == 0 && (Boolean) res.data){
                        comparisontext.setText(R.string.fp_comparison_pass);
                        FPResult = ResultCode.VERIFY_PASS;

                        studentInfo.setRollCallState(FPResult);


                        rcPackage = new ResultPackage();
                        //返回成功状态码
                        comparisontext.setTextColor(Color.GREEN);

                        studentInfo.setFinalRollCalltime(formatter.format(new Date()));
                        try {
                            DataDao.updateInfo(studentInfo);
                        }catch (Exception e){

                        }
                      //  Intent intent = new Intent(MainCheckActivity.this,TransitionActivity.class);
                      //  startActivity(intent);
                        Toast.makeText(MainCheckActivity.this, "成功", Toast.LENGTH_SHORT).show();

                        //传值
                         giveValue();



                   //     finish();

                    }else {
                        Toast.makeText(MainCheckActivity.this, "失败", Toast.LENGTH_SHORT).show();

                        comparisontext.setText(R.string.fp_comparison_pass_not);
                        FPResult = ResultCode.VERIFY_NO_PASS;
                        //返回失败状态码
                        // studentInfo.setRollCallState(FPResult);
                        Toast.makeText(MainCheckActivity.this, res.error+"失败",
                                Toast.LENGTH_SHORT).show();
                        comparisontext.setTextColor(Color.RED);
                        new Handler(new Callback() {
                            @Override
                            public boolean handleMessage(Message msg) {
                                //实现页面跳转
                                Intent intent = new Intent(MainCheckActivity.this,TransitionActivity.class);
                                startActivity(intent);
                                finish();
                                return false;
                            }
                        }).sendEmptyMessageDelayed(0,3000);

                    }
                    clearData();
                    dismissProgressDialog();
                    break;
            }
        }
    };
    public void giveValue(){

        rcPackage.m_strBeginTime = String.valueOf(System.currentTimeMillis()/1000);
        //考生数量
        rcPackage.m_nSporterNum = studentInfos.size();
        for (int i = 0; i< rcPackage.m_nSporterNum; i++){
            //考生准考号
            rcPackage.m_strSporterID[i] = studentInfo.getStudenCode();
            //考生姓名
            rcPackage.m_strSporterName[i]  = studentInfo.getStudentName();
            //性别
            rcPackage.m_nGender[i] = studentInfo.getSex();
            //检录状态
            rcPackage.m_strSporterScoreCheck[i] = String.valueOf(studentInfo.getRollCallState());
        }
   //     new Thread(mRunnable).start();

            textDate();
            HandlerThread thread = new HandlerThread("MyHandlerThread");
            thread.start();//创建一个HandlerThread并启动它
            mHandler = new Handler(thread.getLooper());//使用HandlerThread的looper对象创建Handler，如果使用默认的构造方法，很有可能阻塞UI线程
            mHandler.post(mRunnable);//将线程post到Handler中


    }
    public void textDate(){
        //rcPackage = new ResultPackage();
//        studentInfos = DataDao.queryAll();
//        for (int i = 0;i<studentInfos.size();i++) {
//            studentInfo = studentInfos.get(i);
//        }
        rcPackage.m_strClientName="";
        rcPackage.m_strServerName = "";
        rcPackage.m_nEventType = String.valueOf(TCPConst.enumEvent.EventScore.getIndex());
        rcPackage.m_strPackType = "SendCheckPackage";
        rcPackage.m_strGameEventName = "";
        // rcPackage. m_nSex = 0;
        //组别如：甲组
        rcPackage.m_strSort = "";
        //项目名称如：1000米
        rcPackage. m_strEvent = "";
        //赛次，-预赛，-次赛，-复赛，-决赛
        //rcPackage. m_nLayer =0;
        //分组，如：
        // rcPackage. m_nGrp = 0;
        //项目属性1-径赛，-田高，-田远，-接力，-全能
        //rcPackage.  m_nProperty = 0;
        //全能分项名称
        rcPackage. m_strAllEventName = "";
        //全能分项属性
        //rcPackage.  m_nAllProp  = 0;
        //全能分项序号
        // rcPackage.      m_nAllNum = 0;
        //风速，如：+1.2，-0.21
        rcPackage.m_strWindSpeed = "";
        //场次
        //rcPackage.  m_nField = 0;
        //检录状态
        // rcPackage. m_nCheck = 0;
        //成绩是否确认
        // rcPackage.  m_nAffirmFlag = 0;
        //考试地址
        rcPackage.m_strExamSitePlace = "";
        //分组ID
        //rcPackage.m_nGroupType = 0;

        for (int i = 0; i< rcPackage.m_nSporterNum; i++){
            //名次
            //rcPackage.m_nPlace[i] = 0;
            //道号
            //rcPackage.m_nTrackNO[i]= 0;
            //单位
            rcPackage.m_strUnitName[i] = "";
            rcPackage.m_strResult[i] = "";
            rcPackage.m_strNote[i] ="";


            rcPackage.m_strJudgeTime[i] = "";

            //单位代码
            rcPackage.m_strUnitCode[i] ="";
            //班级
            rcPackage.m_strClassName[i] = "";
            //考生类别
            rcPackage.m_strStudentCategory[i]= "";
            //最好成绩
            rcPackage.m_strBestScore[i] = "";
            //考试状态
            // rcPackage.m_nExamStatus[i] = 0;
        }
        for(int i =0; i<MAX_RECORD_COUNT;i++){
            rcPackage.m_strRecordRank[i] = "";
            rcPackage.m_strRecordResult[i] ="";
        }
    }
    //上传
    Runnable mRunnable=new Runnable() {
        @Override
        public void run() {
            //    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

            String datas = rcPackage.EncodePackage(new PackageHeadInfo(),false, TCPConst.enumCodeType.CodeGB2312.getIndex());
            if (ip == "" || local == ""){
                ip = "127.0.0.1";
                local = "4027";
            }
            new SocketClient().getTCPConnect(ip, Integer.parseInt(local), datas, new SocketClient.CallBackSocketTCP() {

                //返回的消息
                @Override
                public void Receive(final String info) {
                    Log.e("SData:--------------->",info );
                            String target = null;
                            String target1 = null;
                            String target2 = null;
                            try {
                                target1 = new String(g_SepFlag1,"GB2312");
                                target2 = new String(PackageHeadInfo.g_SepFlag,"GB2312");
                                target=target1+target2;
                            } catch (UnsupportedEncodingException e) {
                                e.printStackTrace();
                            }
                            String []  ArrString = info.split(target2);
                            rcPackage.DecodePackage(ArrString,new ResultPackage());
                }
                @Override
                public void isConnect(boolean state) {

                    if(state){
                        Log.e( "连接状态-------->: ",state+"" );
//                        Toast.makeText(MainCheckActivity.this, ""+state, Toast.LENGTH_SHORT).show();
                        stateTextData.post(new Runnable() {
                            @Override
                            public void run() {
                                stateTextData.setText(R.string.tcp_connect);
                                FPResult = ResultCode.VERIFY_PASS;
                                if(studentInfo != null){
                                    studentInfo.setUploadState(FPResult);
                                    studentInfo.setUploadTime(formatter.format(new Date()));
                                    DataDao.updateInfo(studentInfo);
                                }


                            }
                        });
                    }else{
                   //     Toast.makeText(MainCheckActivity.this, "正在尝试重新连接socket", Toast.LENGTH_SHORT).show();
                        stateTextData.post(new Runnable() {
                            @Override
                            public void run() {
                                stateTextData.setText(R.string.tcp_Reconnect);
                            }
                        });
                    }
                }
            });
            new SocketClient().closeSocket();
            // Log.e( "manualQuery: ",datas.toString() );
        }
    };



}